var page = 1,
    isLoading = false;

$(function(){
	$(window).bind('scroll', onScroll);	
});

function onLoadData(data) {
    isLoading = false;
    page++;
    $('.container').append(data);
    setupBlocks();
  };

function onScroll(event) {
  // Only check when we're not still waiting for data.
  if(!isLoading) {
	// Check if we're within 400 pixels of the bottom edge of the broser window.
	var winHeight = window.innerHeight ? window.innerHeight : $(window).height(); // iphone fix
	var closeToBottom = ($(window).scrollTop() + winHeight > $(document).height() - 400);

	if (closeToBottom) {
	  isLoading = true;

	  $.ajax({
	    url: 'ajax.html',
		    data: {page: page},
		    success: onLoadData
	  });
    }
  }
};